<?php
ini_set('date.timezone', 'Asia/Shanghai');
require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$dbInfo = require(__DIR__ . '/../config/mysql.php');
$errInfo = require(__DIR__ . '/../conf/err.php');

$db = new \yii\db\Connection;
$db->dsn = $dbInfo['dsn'];
$db->username = $dbInfo['username'];
$db->password = $dbInfo['password'];

$sql = 'SELECT * FROM easyapi ORDER BY id DESC LIMIT 20';
$res = $db->createCommand($sql);
$list = $res->queryAll();

foreach ($list as $key => $val) {
    // 请求状态
    if ($val['status'] == 0) {
        $list[$key]['statusName'] = '异常';
    } else if ($val['status'] == 1) {
        $list[$key]['statusName'] = '成功';
    } else if ($val['status']) {
        $status = $val['status'];
        if (isset($errInfo[$status])) {
            $list[$key]['statusName'] = $errInfo[$status];
        }
    }
    // 请求时间
    $req_time = substr($val['req_time'], 0, 10);
    $list[$key]['req_date'] = date("Y-m-d H:i:s", $req_time) . ' ' . substr($val['req_time'], 10);
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>DEBUG</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <!--标准mui.css-->
    <link rel="stylesheet" href="https://cdn.bootcss.com/mui/3.7.1/css/mui.min.css">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<header class="mui-bar mui-bar-nav">
    <h1 class="mui-title" style="color: crimson;font-weight: bold;">HTTP LOG</h1>
</header>
<div class="mui-content">
    <?php foreach ($list as $key => $val) { ?>
        <div class="mui-card" style="margin-bottom: 35px;">
            <ul class="mui-table-view">
                <li class="mui-table-view-cell mui-media mui-collapse">
                    <a href="javascript:;" class="mui-navigate-right">
                        <div class="mui-media-body" style="font-size: 12px;">
                            <?php
                            if ($val['status'] == 1) {
                                echo "<h5>请求时间: " . $val['req_date'] . "</h5><span style=\"color: green;min-width:300px;font-weight: bold;\">" . $val['route'] . ' - ' . $val['statusName'] . "</span>";
                            } else {
                                echo "<h5>请求时间: " . $val['req_date'] . "</h5><span style=\"color: red;min-width:300px;font-weight: bold;\">" . $val['route'] . ' - ' . $val['statusName'] . "</span>";
                            }
                            ?>
                        </div>
                    </a>
                    <div class="mui-collapse-content" style="color:#444;">
                        <?php

                        echo '<pre contenteditable="true">';
                        echo '接收数据:<br />';
                        echo $val['req_data'];

                        echo '<br />-------------------------------------------------<br />';
                        echo '返回数据:<br />';
                        print_r(json_decode($val['res_data'], true));

                        echo '-------------------------------------------------<br />';
                        echo '接口信息:<br />';
                        $data = array();
                        $data['日志ID'] = $val['id'];
                        $data['客户端IP'] = $val['ip'];
                        $data['接口耗时'] = $val['use_time'] . 'ms';
                        $data['用户ID'] = $val['user_id'];
                        print_r($data);

                        echo '</pre>';
                        ?>
                    </div>
                </li>
            </ul>
        </div>
    <?php } ?>
</div>
</body>
<script src="https://cdn.bootcss.com/mui/3.7.1/js/mui.min.js"></script>
</html>